package com.hqs.rxjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hqs.common.utils.Log;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jakewharton.rxbinding2.widget.TextViewAfterTextChangeEvent;
import com.jakewharton.rxbinding2.widget.TextViewTextChangeEvent;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.plugins.RxAndroidPlugins;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends AppCompatActivity {

    private TextView tvResult;
    private EditText etName;
    private EditText etPwd;
    private Observable observable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initRxJava();
    }

    private void initView(){
        tvResult = (TextView) findViewById(R.id.tv_result);
        etName = (EditText) findViewById(R.id.et_name);
        etPwd = (EditText) findViewById(R.id.et_pwd);
    }

    private void initRxJava(){

        observable = RxTextView.textChangeEvents(etName)
                .subscribeOn(Schedulers.io())
                // debounce 需要在observeOn之前调用
                .debounce(1000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread());

        observable.subscribe(new Observer<TextViewTextChangeEvent>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull TextViewTextChangeEvent textViewTextChangeEvent) {

                        if (textViewTextChangeEvent.text() != null){
                            String text = textViewTextChangeEvent.text().toString();
                            if (text.length() < 3){
                                tvResult.setText("name is too short " + text.length());
                            }
                            else{
                                tvResult.setText("name is ok " + text.length());
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    public void loginAction(View view){

        // 模拟登录
        tvResult.setText("正在登录...");

        Observable.create(new ObservableOnSubscribe<String>() {

            @Override
            public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
                Thread.sleep(1000);
                if (etName.getText().toString().equals(etPwd.getText().toString())){
                    e.onNext("登录成功");
                }
                else{
                    e.onNext("登录失败");
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        tvResult.setText(s);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        observable.unsubscribeOn(Schedulers.io());
    }
}
